package com.kitty.game.welfare.service;

import com.kitty.common.utils.SpringUtils;
import com.kitty.game.i18n.I18nId;
import com.kitty.game.role.model.Role;
import com.kitty.common.model.Reason;
import com.kitty.mina.message.MessagePusher;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Calendar;

/**在线奖励*/
@Component
public class OnlineRewardHandler {
    /**当天的生效开始时间，每天0点重新计算*/
    private long startTime;
    /**当天的生效结束时间，每天0点重新计算*/
    private long endTime;

    @PostConstruct
    private void init() {
        initTime();
    }

    /**初始每天的生效开始时间和结束时间*/
    public void initTime() {
        /**生效时间段05:00-22:00*/
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 5);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        startTime = calendar.getTimeInMillis();

        calendar.set(Calendar.HOUR_OF_DAY, 22);
        endTime = calendar.getTimeInMillis();
    }

    public void handleHeartBeart(Role role, long prevHeartTime) {
        if (role == null) {
            return;
        }

        long now = System.currentTimeMillis();
        /**在时间段内才加在线领奖的时长*/
        if (now < startTime || now > endTime) {
            return ;
        }

        if (prevHeartTime > 0) {
            /**累计在线时长*/
//            int sec = (int) ((now - prevHeartTime) / TimeUtil.ONE_SECOND);
//            role.getExtendBox().setOnlineRewardSecCahce(sec + role.getExtendBox().getOnlineRewardSecCahce());
//            role.save();
        }
    }

    /**把昨天累计的在线时长赋值*/
    public void setOnlineRewardSec(Role role) {
        if (role.getExtendBox().getOnlineRewardSecCahce() > 0) {
            role.getExtendBox().setOnlineRewardSec(role.getExtendBox().getOnlineRewardSecCahce());
            role.getExtendBox().setOnlineRewardSecCahce(0);
            role.save();
        }
    }

    /**
     * 领取在线奖励
     */
    public void fetchReward(Role role) {
        /**已经领取*/
        if (role.getExtendBox().getOnlineRewardSec() < 0) {
            MessagePusher.notify2Player(role, I18nId.PMT_2200);
            return ;
        }

        /**在线时间不足*/
        int minute =role.getExtendBox().getOnlineRewardSec() / 60;
        /**60分钟以上才能领取奖励*/
        if (minute < 60) {
            MessagePusher.notify2Player(role, I18nId.PMT_2201);
            return ;
        }

        /**每分钟10银元宝*/
        int silver = minute * 10;

        SpringUtils.getRoleService().addSiver(role, silver, Reason.ONLINE_REWARD);
        /**设置已领取*/
        role.getExtendBox().setOnlineRewardSec(-1);
        role.save();
        MessagePusher.notifyMiscEx2Player(role, I18nId.PMT_2202, silver);

//        if (silver > 6000) {
//            String content = I18nIdDataPool.getI18nContent(I18nId.PMT_2203, role.getName());
//            SpringUtils.getChatService().sendNumor(content, Const.BRODCAST_MSG_TYPE_ROLE);
//        }
    }

    /**
     * 发送系统消息
     */
    public void sendSystem() {
//        String content = I18nIdDataPool.getI18nContent(I18nId.PMT_2204);
//        SpringUtils.getChatService().sendSystem(content, Const.BRODCAST_MSG_TYPE_ROLE);
    }
}
